package com.superhelper.common.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsRequest;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsResponse;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByVoiceRequest;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByVoiceResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.superhelper.common.utils.JsonHelper;

public class VmsService {
	private final static Logger LOG = LoggerFactory.getLogger(VmsService.class);
	// 产品名称:云通信语音API产品,开发者无需替换
	private final String product = "Dyvmsapi";
	// 产品域名,开发者无需替换
	private final String domain = "dyvmsapi.aliyuncs.com";
	private final String region = "cn-hangzhou";
	private final String accessKeyId = "LTAIayggS1c9jA2q";
	private final String accessKeySecret = "u7XDsuMBjLvkzdn3o6Sct7PhEQQ5Kw";
	private final String showNumber = "02066200001";
	private final String tts_id = "TTS_77615062";
	private IAcsClient acsClient;

	public VmsService() throws ClientException {
		// 可自助调整超时时间
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");

		// 初始化acsClient,暂不支持region化
		IClientProfile profile = DefaultProfile.getProfile(region, accessKeyId,
				accessKeySecret);
		DefaultProfile.addEndpoint(region, region, product, domain);
		acsClient = new DefaultAcsClient(profile);
	}

	/**
	 * 语音文本外呼
	 * 
	 * @return
	 * @throws ClientException
	 */
	public SingleCallByTtsResponse singleCallByTts(String phone)
			throws ClientException {

		// 组装请求对象-具体描述见控制台-文档部分内容
		SingleCallByTtsRequest request = new SingleCallByTtsRequest();
		// 必填-被叫显号,可在语音控制台中找到所购买的显号
		request.setCalledShowNumber(showNumber);
		// 必填-被叫号码
		request.setCalledNumber(phone);
		// 必填-Tts模板ID
		request.setTtsCode(tts_id);
		// 可选-当模板中存在变量时需要设置此值
		// if (StringUtils.isNoneBlank(TtsParam)) {
		// request.setTtsParam(TtsParam);
		// }
		// 可选-外部扩展字段,此ID将在回执消息中带回给调用方
		request.setOutId("yourOutId");
		// hint 此处可能会抛出异常，注意catch
		SingleCallByTtsResponse singleCallByTtsResponse = acsClient
				.getAcsResponse(request);
		LOG.info("SingleCallByTtsResponse:{}",
				JsonHelper.toJson(singleCallByTtsResponse));
		return singleCallByTtsResponse;
	}

	/**
	 * 语音文件外呼
	 * 
	 * @return
	 * @throws ClientException
	 */
	public SingleCallByVoiceResponse singleCallByVoice(String phone,
			String vFileId, String TtsParam) throws ClientException {

		// 组装请求对象-具体描述见控制台-文档部分内容
		SingleCallByVoiceRequest request = new SingleCallByVoiceRequest();
		// 必填-被叫显号,可在语音控制台中找到所购买的显号
		request.setCalledShowNumber(showNumber);
		// 必填-被叫号码
		request.setCalledNumber(phone);
		// 必填-语音文件ID
		request.setVoiceCode(vFileId);
		// 可选-外部扩展字段
		request.setOutId("yourOutId");

		// hint 此处可能会抛出异常，注意catch
		SingleCallByVoiceResponse singleCallByVoiceResponse = acsClient
				.getAcsResponse(request);
		LOG.info("singleCallByVoiceResponse:{}",
				JsonHelper.toJson(singleCallByVoiceResponse));
		return singleCallByVoiceResponse;
	}

	public static void main(String[] args) throws ClientException,
			InterruptedException {
		VmsService vms = new VmsService();
		SingleCallByTtsResponse singleCallByTtsResponse = vms
				.singleCallByTts("13533138776");
		System.out.println("语音文本外呼---------------");
		System.out.println("RequestId="
				+ singleCallByTtsResponse.getRequestId());
		System.out.println("Code=" + singleCallByTtsResponse.getCode());
		System.out.println("Message=" + singleCallByTtsResponse.getMessage());
		System.out.println("CallId=" + singleCallByTtsResponse.getCallId());

		Thread.sleep(20000L);

		// SingleCallByVoiceResponse singleCallByVoiceResponse = vms
		// .singleCallByVoice();
		// System.out.println("语音文件外呼---------------");
		// System.out.println("RequestId="
		// + singleCallByVoiceResponse.getRequestId());
		// System.out.println("Code=" + singleCallByVoiceResponse.getCode());
		// System.out.println("Message=" +
		// singleCallByVoiceResponse.getMessage());
		// System.out.println("CallId=" +
		// singleCallByVoiceResponse.getCallId());
	}

}
